#!/bin/bash
# logstash 快捷docker安装运行
# 创建目录并移动脚本的函数
function createStart() {
    local dockerVolumes="$1"
    local name="$2"
    local port="$3"
    local scriptName="${name}.sh"
    local destination="/${dockerVolumes}/${name}/"
    rm -rf "${destination}/${port}"
    # 为脚本添加执行权限
    chmod +x "${scriptName}"

    # 使用 mkdir -p 创建父目录（如果不存在）
    mkdir -p "${destination}/${port}"

    # 删除已存在的脚本文件
    [ -f "/${dockerVolumes}/${name}/${scriptName}" ] && rm -rf "/${dockerVolumes}/${name}/${scriptName}"

    # 在移动之前检查脚本文件是否存在
    if [ -f "${scriptName}" ]; then
        cp "${scriptName}" "/${dockerVolumes}/${name}/"
        echo "脚本 '${scriptName}' 已复制到 '/${dockerVolumes}/${name}/'"
    else
        echo "错误：当前目录中找不到脚本 '${scriptName}'"
        exit 1
    fi
}
function dockerCreateStart() {
    name="$1"
    version="$2"
    asName="$3"
    portMap=$4
    volumesMap=$5
    dockerMap=$6
    copyDockerMap=$7
    lastDockerMap=$8
    #MYSQL_ROOT_PASSWORD=$9

    # 拉取 Docker 镜像（取消注释以启用）
    docker pull "${name}:${version}"

    # 删除同名容器
    docker rm -f "${asName}" &>/dev/null

    dockerRun="docker run "
    dockerRunTemp="docker run "


    if [ "${#portMap[@]}" -eq 0 ]; then
        echo ""
    else
        # 添加端口映射参数
        for element in "${portMap[@]}"; do
            dockerRun+="-p $element "
            dockerRunTemp+="-p $element "
        done
    fi

    if [ "${#dockerMap[@]}" -eq 0 ]; then
        echo ""
    else
        # 添加其他 Docker 参数
        for element in "${dockerMap[@]}"; do
            dockerRun+="$element "
            dockerRunTemp+="$element "
        done
    fi


    if [ "${#volumesMap[@]}" -eq 0 ]; then
        echo ""
    else
        # 添加卷映射参数
        for element in "${volumesMap[@]}"; do
            dockerRun+="-v $element "
        done
    fi

    dockerRun+="${name}:${version} "
    dockerRunTemp+="${name}:${version} "

    # 添加镜像及额外参数
    if [ "${#lastDockerMap[@]}" -eq 0 ]; then
        echo ""
    else
        for element in "${lastDockerMap[@]}"; do
            dockerRun+=" $element "
            dockerRunTemp+=" $element "
        done
    fi

    echo "$dockerRunTemp"
    eval "$dockerRunTemp"

    if [ "${#copyDockerMap[@]}" -eq 0 ]; then
        echo ""
    else
        echo "复制 开始"
        # 复制 Docker 映射文件
        for element in "${copyDockerMap[@]}"; do
            echo "复制 $element"
            IFS=':' read -r source dest <<< "$element"
            echo "docker cp ${asName}:${source} ${dest}"
            docker cp "${asName}":"${source}" "${dest}"
        done

        echo "复制 完成"
    fi


    # 删除同名容器
    docker rm -f "${asName}" &>/dev/null

    echo "正式启动挂载"
    echo "$dockerRun"
    eval "$dockerRun"
    #$dockerRun

    #read -r -p "查看 $name 运行日志?【Y/N】" input
    #if [ "$input,," =~ ^(YES|Y)$ ]; then
    #    docker logs -f -t ${asName}
    #else
    #    echo ""
    #fi
}
function main(){
    #docker pull logstash:7.14.1
    # 默认值
    networkName="common-network"
    dockerVolumes="docker-volumes"
    #镜像名
    name="logstash"
    #别名
    asName="logstash"
    #版本
    version="7.14.1"
    port="9000"
    port2="4560"
    #将容器  端口映射到宿主机的  端口
    portMap=("$port:9000" "$port2:4560" )

    #挂载地址
    v0="/etc/localtime:/etc/localtime"
    v1="/"${dockerVolumes}"/${asName}/${port}/${asName}/config:/usr/share/${asName}/config"
    #v2="/"${dockerVolumes}"/${asName}/data:/usr/share/${asName}/data"
    v3="/"${dockerVolumes}"/${asName}/${port}/${asName}/messages:/var/log/messages"
    v4="/"${dockerVolumes}"/${asName}/${port}/${asName}/pipeline:/usr/share/logstash/pipeline"
    volumesMap=("$v0" "$v1" "$v3" "$v4")
    #环境配置
    dockerMap=("-d" "--name=${asName}" "--network=${networkName}")
    #dockerMap+=("--privileged")

    lastDockerMap=()
    #lastDockerMap+=("")
    #容器地址:宿主机地址
    cp1="/usr/share/logstash:/$dockerVolumes/$asName/$port"
    cp2="/var/log/messages:/$dockerVolumes/$asName/$port/$asName"

    copyDockerMap=("$cp1" "$cp2")

    # 输出关于设置的信息
    echo "============================================="
    echo "        ${name} Docker 安装挂载启动          "
    echo "============================================="
    docker network create "$networkName"
    # 使用 createStart 函数创建挂载目录
    echo "创建挂载目录..."
    createStart "${dockerVolumes}" "${asName}" "${port}"

    # 使用 dockerCreateStart 函数运行 Docker 容器
    dockerCreateStart "${name}" "${version}" "${asName}"   $portMap $volumesMap $dockerMap $copyDockerMap $lastDockerMap

    echo "${version} ik 分词器 安装"
    mkdir "${asName}"
    pathOne=$PWD
    cd "${asName}"

    if [ -f "ml-latest-small.zip" ];then
        echo "本地 ${pathOne}/${asName} 中存在 ml-latest-small.zip "
    else
        wget "https://files.grouplens.org/datasets/movielens/ml-latest-small.zip"
    fi

    mkdir /"${dockerVolumes}"/"${asName}"/tempelllllllll
    cp "ml-latest-small.zip" /"${dockerVolumes}"/"${asName}"/tempelllllllll/"ml-latest-small.zip"
    cp "${asName}-sample.conf" /"${dockerVolumes}"/"${asName}"/tempelllllllll/"${asName}-sample.conf"
    cd  /"${dockerVolumes}"/"${asName}"/tempelllllllll
    unzip "ml-latest-small.zip"
    #/usr/share/logstash/config/logstash-sample.conf
    mv "movies.csv" /"${dockerVolumes}"/"${asName}"/data/"movies.csv"
    mv "${asName}-sample.conf" /"${dockerVolumes}"/"${asName}"/config/"${asName}-sample.conf"
    rm -rf  /"${dockerVolumes}"/"${asName}"/tempelllllllll
    pwd

    cd $pathOne
    echo "安装完成"

    docker restart ${asName}

    echo "============================================="
    echo "        ${name} Docker 安装挂载启动成功         "
    echo "============================================="
    for element in "${volumesMap[@]}"; do
    echo "        ${asName} Docker 挂载目录：            "
        echo ${element}
    done
    echo "============================================="
}
main